<?php

class RespondentBuilder {
	
	public function create($data) {
    $db = Db::getConnection();
		$db->start();
    $coreData = array(
    	'country' => $data['country'],
      'state' => $data['state'],
      'age' => $data['age'],
      'experience' => $data['experience'],
      'industry' => $data['industry'],
      'employees' => $data['employees'],
      'occupation' => $data['occupation'],
      'developers' => $data['developers'],
      'team_size' => $data['team_size'],
      'os' => $data['os'],
      'changejobs' => $this->yesNo($data['changejobs']),
      'satisfaction' => $this->satisfaction($data['satisfaction']),
      'compensation' => $this->compensation($data['compensation']),
      'compensation_string' => $data['compensation'],
      'so_reputation' => $data['so_rep']
    );
		$id = $db->insert('respondent', $coreData);
		
		$techData = array(
			'id_respondent' => $id,
			'c' => $this->choice($data['tech_c'],'C',''),
			'cpp' => $this->choice($data['tech_cpp'],'C++',''),
			'cs' => $this->choice($data['tech_cs'],'C#',''),
			'java' => $this->choice($data['tech_java'],'Java',''),
			'javascript' => $this->choice($data['tech_javascript'],'JavaScript',''),
			'jquery' => $this->choice($data['tech_jquery'],'jQuery','')|$this->choice($data['tech_jq'],'JQuery',''),
			'node' => $this->choice($data['tech_node'],'Node.js',''),
			'objc' => $this->choice($data['tech_objc'],'Objective-C',''),
			'php' => $this->choice($data['tech_php'],'PHP',''),
			'python' => $this->choice($data['tech_python'],'Python',''),
			'ruby' => $this->choice($data['tech_ruby'],'Ruby',''),
			'sql' => $this->choice($data['tech_sql'],'SQL',''),
			'other' => $data['tech_other'],
			'exc_node' => $this->choice($data['exc_node'], 'Node.js',''),
			'exc_haskell' => $this->choice($data['exc_haskell'], 'Haskell',''),
			'exc_coffee' => $this->choice($data['exc_coffeescript'], 'CoffeeScript',''),
			'exc_dart' => $this->choice($data['exc_dart'], 'Dart',''),
			'exc_typescript' => $this->choice($data['exc_typescript'], 'TypeScript',''),
			'exc_cpp' => $this->choice($data['exc_cpp11'], 'C++11',''),
			'exc_winrt' => $this->choice($data['exc_winrt'], 'WinRT',''),
			'exc_redis' => $this->choice($data['exc_redis'], 'Redis',''),
			'exc_mongo' => $this->choice($data['exc_mongodb'], 'MongoDB',''),
			'exc_fs' => $this->choice($data['exc_fs'], 'F#',''),
			'exc_phonegap' => $this->choice($data['exc_phonegap'], 'PhoneGap',''),
		);
		$db->insert('technology', $techData);
		
		$impData = array(
			'id_respondent' => $id,
			'compensation' => $this->importance($data['char_compensation']),
			'stock' => $this->importance($data['char_stock']),
			'goals' => $this->importance($data['char_goals']),
			'excitement' => $this->importance($data['char_excitement']),
			'newtech' => $this->importance($data['char_newtech']),
			'growth' => $this->importance($data['char_growth']),
			'office' => $this->importance($data['char_space']),
			'team' => $this->importance($data['char_team']),
			'structure' => $this->importance($data['char_structure']),
   		'control' => $this->importance($data['char_control']),
		  'workstation' => $this->importance($data['char_workstation']),
   		'autonomy' => $this->importance($data['char_autonomy']),
   		'location' => $this->importance($data['char_location']),
   		'commute' => $this->importance($data['char_commute']),
	   	'fourty' => $this->importance($data['char_hours']),
   		'weekends' => $this->importance($data['char_nights']),
		);		
		$db->insert('importance', $impData);
		
		$timeData = array(
			'id_respondent' => $id,
			'newdev' => $this->time($data['time_newdev']),
			'refactor' => $this->time($data['time_refactor']),
			'bugs' => $this->time($data['time_bugs']),
			'support' => $this->time($data['time_support']),
			'meetings' => $this->time($data['time_meetings']),
			'skills' => $this->time($data['time_skills']),
			'surfing' => $this->time($data['time_surf']),
			'commute' => $this->time($data['time_commute'])	
		);
		$db->insert('time', $timeData);
		
		$timeStringData = array(
			'id_respondent' => $id,
			'newdev' => $data['time_newdev'],
			'refactor' => $data['time_refactor'],
			'bugs' => $data['time_bugs'],
			'support' => $data['time_support'],
			'meetings' => $data['time_meetings'],
			'skills' => $data['time_skills'],
			'surfing' => $data['time_surf'],
			'commute' => $data['time_commute']	
		);
		$db->insert('time_strings', $timeStringData);
		
		$db->commit();
		return $id;
	}

	private function time($value) {
  	switch ($value) {
  		case "None":
  			return 0;
  		case "1-2 hours":
  			return 1.5;
  		case "2-5 hours":
  			return 3.5;
  		case "5-10 hours":
  			return 7.5;
  		case "10-20 hours":
  			return 15;
  		case "20+ hours":
  			return 25;
  		default:
  			return null;
  	}
  }
  
  private function yesNo($value) {
  	return $this->choice($value, 'Yes', 'No');
  }
  
  private function choice($value, $pos, $neg) {
  	if ($value == $pos) return 1;
  	else if ($value == $neg) return 0;
  	return null;
  }
  
  private function compensation($value) {
  	switch ($value) {
 			case "<$20,000":
 				return 20000;
 			case "$20,000 - $40,000":
 				return 30000;
 			case "$40,000 - $60,000":
 				return 50000;
 			case "$60,000 - $80,000":
 				return 70000;
 			case "$80,000 - $100,000":
 				return 90000;
	 		case "$100,000 - $120,000":
 				return 110000;
	 		case "$120,000 - $140,000":
	 			return 130000;
 			case ">$140,000":
 				return 160000;
  		default:
  			return null;
  	}
  }

  private function satisfaction($value) {
  	switch ($value) {
    case "Hate my job":
    	return 1;
    case "I'm not happy in my job":
    	return 2;
    case "It's a paycheck":
    	return 3;
    case "I enjoy going to work":
    	return 4;
    case "Love my job":
    	return 5;
    default:
    	return null;
		}
  }
  
  private function importance($value) {
  	switch ($value) {
  	case "Non-Negotiable":
  		return 5;
  	case "Very Important":
  		return 4;
  	case "Somewhat Important":
  		return 3;
  	case "Not Very Important":
  		return 2;
  	case "Don't Care":
  		return 1;
  	default:
  		return null;
  	}
	}
}
